﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space
{
    public class Building : Modelled, ITarget
    {
        public bool Alive { get; set; }
        public float HP { get; set; }
        
        public SolarSystem SolarSystem { get; set; }

        public string EffectName { get; set; }
        public string EffectArgument { get; set; }

        public Player Player { get; set; }

        public string UpgradeTo { get; set; }

        public string Category { get; set; }
        public int Level { get; set; }

        public float Mass { get; set; }

        public int CreditCost { get; set; }

        public int Value { get { return CreditCost; } }

        public Game Game { get; set; }
        public Building(Game game)
        {
            Game = game;
            Alive = true;
            UpgradeTo = "";
        }

        public void Update(float time)
        {
            float circleSpeed = 1 / (float)Math.Sqrt(Math.Pow((Position - SolarSystem.Position).Length(), 3) / SolarSystem.Mass);
            Position = Vector3.Transform(Position, Matrix.CreateTranslation(-SolarSystem.Position) * Matrix.CreateRotationY(circleSpeed * time) * Matrix.CreateTranslation(SolarSystem.Position));
        }
        public void Draw()
        {
            ICamera camera = Game.Services.GetService<ICamera>();

            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (Effect effect in mesh.Effects)
                {
                    effect.CurrentTechnique = effect.Techniques["Technique1"];
                    effect.Parameters["World"].SetValue(Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position));
                    effect.Parameters["Projection"].SetValue(camera.Projection);
                    effect.Parameters["View"].SetValue(camera.View);
                    effect.Parameters["DiffuseColor"].SetValue(Color.White.ToVector3());
                    effect.Parameters["EmissiveColor"].SetValue(Color.Black.ToVector3());
                    effect.Parameters["Light1Direction"].SetValue(Vector3.Normalize(SolarSystem.Position - Position));
                    effect.Parameters["Texture"].SetValue(Texture);
                }
                mesh.Draw();
            }
        }
        public void TakeDamage(float damage)
        {
            HP -= damage;
            if (HP <= 0)
                Alive = false;
        }

        public static int MaxLevel(string type)
        {
            switch(type) {
                case "trade":
                    return 3;
                default:
                    throw new Exception("Unknown type: " + type);
            }
        }
        public static Building Create(Game game, string type, int level)
        {
            Building building;
            switch (type)
            {
                case "trade":
                    building = new Building(game)
                    {
                        HP = 3000 * level,

                        EffectName = "trade_multiply",
                        EffectArgument = (0.3 * level).ToString(),
                        Model = game.Content.Load<Model>("models/tradeCenter"),
                        Scale = 0.5f + 0.5f * level,

                        Category = "trade",
                        Level = level,

                        CreditCost = level * 1000
                    };
                    break;
                case "farm":
                    building = new Building(game)
                    {
                        HP = 3000 * level,

                        EffectName = "growth_multiply",
                        EffectArgument = (0.3 * level).ToString(),
                        Model = game.Content.Load<Model>("models/farmCenter"),
                        Scale = 0.5f + 0.5f * level,

                        Category = "farm",
                        Level = level,

                        CreditCost = level * 1000
                    };
                    break;
                default:
                    throw new Exception("Unknown type");
            }

            return building;
        }
    }
}
